package c.e.b.h.e.o;

import c.e.b.h.e.m.x.h;
import c.e.b.h.e.s.e;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/* compiled from: CrashlyticsReportPersistence */
public class g {
    public static final Charset g = Charset.forName("UTF-8");
    public static final int h = 15;
    public static final h i = new h();
    public static final Comparator<? super File> j = e.a;
    public static final FilenameFilter k = f.a;
    public final AtomicInteger a = new AtomicInteger(0);
    public final File b;
    public final File c;
    public final File d;
    public final File e;
    public final e f;

    public g(File file, e eVar) {
        File file2 = new File(file, "report-persistence");
        this.b = new File(file2, "sessions");
        this.c = new File(file2, "priority-reports");
        this.d = new File(file2, "reports");
        this.e = new File(file2, "native-reports");
        this.f = eVar;
    }

    public static List<File> a(List<File>... listArr) {
        ArrayList arrayList = new ArrayList();
        int i = 0;
        for (List size : listArr) {
            i += size.size();
        }
        arrayList.ensureCapacity(i);
        for (Collection addAll : listArr) {
            arrayList.addAll(addAll);
        }
        return arrayList;
    }

    public static List<File> d(File file) {
        return f(file, null);
    }

    public static List<File> f(File file, FileFilter fileFilter) {
        if (!file.isDirectory()) {
            return Collections.emptyList();
        }
        Object[] listFiles = fileFilter == null ? file.listFiles() : file.listFiles(fileFilter);
        return listFiles != null ? Arrays.asList(listFiles) : Collections.emptyList();
    }

    public static List<File> g(File file, FilenameFilter filenameFilter) {
        if (!file.isDirectory()) {
            return Collections.emptyList();
        }
        Object[] listFiles = filenameFilter == null ? file.listFiles() : file.listFiles(filenameFilter);
        return listFiles != null ? Arrays.asList(listFiles) : Collections.emptyList();
    }

    public static File i(File file) throws IOException {
        Object obj = (file.exists() || file.mkdirs()) ? 1 : null;
        if (obj != null) {
            return file;
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("Could not create directory ");
        stringBuilder.append(file);
        throw new IOException(stringBuilder.toString());
    }

    public static String j(File file) throws IOException {
        byte[] bArr = new byte[8192];
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        FileInputStream fileInputStream = new FileInputStream(file);
        while (true) {
            try {
                int read = fileInputStream.read(bArr);
                if (read > 0) {
                    byteArrayOutputStream.write(bArr, 0, read);
                } else {
                    String str = new String(byteArrayOutputStream.toByteArray(), g);
                    fileInputStream.close();
                    return str;
                }
            } catch (Throwable unused) {
            }
        }
    }

    public static void k(File file) {
        if (file != null) {
            if (file.isDirectory()) {
                for (File k : file.listFiles()) {
                    k(k);
                }
            }
            file.delete();
        }
    }

    public static void l(File file, String str) throws IOException {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file), g);
        try {
            outputStreamWriter.write(str);
            outputStreamWriter.close();
        } catch (Throwable unused) {
        }
    }

    public void b() {
        Iterator it = ((ArrayList) e()).iterator();
        while (it.hasNext()) {
            ((File) it.next()).delete();
        }
    }

    public void c(String str) {
        a aVar = new a(str);
        Iterator it = ((ArrayList) a(g(this.c, aVar), g(this.e, aVar), g(this.d, aVar))).iterator();
        while (it.hasNext()) {
            ((File) it.next()).delete();
        }
    }

    public final List<File> e() {
        List[] listArr = new List[2];
        r2 = new List[2];
        int i = 0;
        r2[0] = d(this.c);
        r2[1] = d(this.e);
        listArr[0] = a(r2);
        listArr[1] = d(this.d);
        while (i < 2) {
            Collections.sort(listArr[i], j);
            i++;
        }
        return a(listArr);
    }

    public final File h(String str) {
        return new File(this.b, str);
    }
}
